//
//  FieldOrderDetailViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/9/10.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "CommonOrderDetailModel.h"

#import "SerOrderDetailStatusCell.h"
#import "SerOrderDetailContactCell.h"
#import "SerOrderDetailSerCell.h"
#import "SerOrderDetailMarkCell.h"
#import "SerOrderDetailInfoCell.h"
#import "OrderDetailNoCopyCell.h"


#import "SerOrderDetailPackageCell.h"
#import "SerOrderDetailFieldCell.h"
#import "SerOrderDetailMenuCell.h"
#import "V2MenuSingleCell.h"

#import "OrderDishesHeaderView.h"
#import "OrderDetailContactSectionFooter.h"

#import "FieldOrderDetailViewController.h"

@interface FieldOrderDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) CommonOrderDetailModel *model;

@property (nonatomic, strong) NSMutableArray<V2CommonMenuModel *> *selDishesArray;

@property (nonatomic, assign) BOOL isFold;

@end

@implementation FieldOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_orderDetail];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initView {

    self.isFold = YES;

    self.title = @"订单详情";
    self.view.backgroundColor = UIColorHex(0xf1f1f1);

    [self.view addSubview:self.tableview];

    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
    
}


#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return 9;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case FieldOrderDetailSectionType_status:{
            return 1;
        } break;
        case FieldOrderDetailSectionType_address:{
            return 2;
        } break;
        case FieldOrderDetailSectionType_server:{
            return 2;
        } break;
        case FieldOrderDetailSectionType_dishes:{
            if (self.isFold) {
                if (self.selDishesArray.count>3) {
                    return 3;
                } else {
                    return self.selDishesArray.count;
                }
            } else {
                return self.selDishesArray.count;
            }
            
        } break;
        case FieldOrderDetailSectionType_mark:{
            return 1;
        } break;
        case FieldOrderDetailSectionType_payDetails:{
            return 5;
        } break;
        case FieldOrderDetailSectionType_orderInfo:{
            return 2;
        } break;
        default:
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case FieldOrderDetailSectionType_status:{
            return 60;
        } break;
        case FieldOrderDetailSectionType_address:{

            switch (indexPath.row) {
                case 0:{
                    return 50;
                } break;
                case 1:{
                    
                    if (self.model.uodOrderType == 3) {
                        return 50;
                    } else {
                        return CGFLOAT_MIN;
                    }
                    
                } break;
                default:
                    break;
            }

        } break;
        case FieldOrderDetailSectionType_server:{
            return 130;
        } break;
        case FieldOrderDetailSectionType_dishes:{
            return 70;
        } break;
        case FieldOrderDetailSectionType_mark:{
            
            
            return [Utilities getStringHeightWithText:self.model.uoRemark font:[UIFont systemFontOfSize:14] viewWidth:(TTScreenWidth-100)] + 20 ;
            
        } break;
        case FieldOrderDetailSectionType_payDetails:{
            
            switch (indexPath.row) {
                case FieldOrderPayDetailCellType_payType:
                case FieldOrderPayDetailCellType_price:{
                    return CGFLOAT_MIN;

                } break;
                case FieldOrderPayDetailCellType_coupon:{
                    return 50;

                } break;
                case FieldOrderPayDetailCellType_pingtaijintie:{
                    return 50;

                } break;
                case FieldOrderPayDetailCellType_totalPay:{
                    return 50;
                                        
                } break;
                default:
                    break;
            }
            
            return 50;
        } break;
        case FieldOrderDetailSectionType_orderInfo:{
            return 50;
        } break;
        default:
            break;
    }
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == FieldOrderDetailSectionType_dishes) {
        return 40;
    }
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == FieldOrderDetailSectionType_server) {
        return 40;
    }
    return CGFLOAT_MIN;
}

#pragma mark sectionheader
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == FieldOrderDetailSectionType_dishes) {
        OrderDishesHeaderView *headView  =  [[NSBundle mainBundle] loadNibNamed:@"OrderDishesHeaderView"owner:nil options:nil].firstObject;
        headView.lineView.hidden = NO;
        headView.mainTitleLab.text = [NSString stringWithFormat:@"包含菜品（%ld）",self.selDishesArray.count];
        [headView fillViewWithFold:self.isFold];
        headView.backgroundColor = UIColor.whiteColor;
        
        WEAKSELF
        headView.OrderDishesHeaderViewBlock = ^(BOOL isFold) {
            weakSelf.isFold = isFold;
            [weakSelf.tableview reloadData];
        };
        
        return  headView;
    }
    
    return  [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    if (section == FieldOrderDetailSectionType_server) {
        OrderDetailContactSectionFooter *footerView  =  [[NSBundle mainBundle] loadNibNamed:@"OrderDetailContactSectionFooter"owner:nil options:nil].firstObject;
        
        WEAKSELF
        footerView.ServiceCatSubSegNaviBlock = ^(OrderDetailContactActionType type) {
          
            switch (type) {
                case OrderDetailContactActionType_im:{
                    
                    [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.model.uodSellerId];
                } break;
                case OrderDetailContactActionType_call:{
                    
                    [[TT_PhonaCallManager manager] callWithStoreId:weakSelf.model.uodSellerId];
                } break;
                default:
                    break;
            }
        };
        return footerView;
    }
    
    return  [UIView new];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case FieldOrderDetailSectionType_status:{

            SerOrderDetailStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailStatusCellID"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fillCellWithModel:self.model];
            return cell;

        } break;
        case FieldOrderDetailSectionType_address:{

            switch (indexPath.row) {
                case 0:{
                    
                    SerOrderDetailContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailContactCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    cell.mainTitle.text = self.model.uoUserName;
                    cell.contentLab.text = self.model.uoUserTel;
                    return cell;
                    
                } break;
                case 1:{
                    
                    SerOrderDetailContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailContactCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.mainTitle.text = @"服务时间";
                    cell.contentLab.text = self.model.uodServiceTime;
                    return cell;
                    
                } break;
                default:
                    break;
            }
            


        } break;
        case FieldOrderDetailSectionType_server:{

            switch (indexPath.row) {
                case 0:{
                    SerOrderDetailFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailFieldCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    [cell fillCellWithModel:self.model];
                    return cell;
                } break;
                case 1:{
                    SerOrderDetailMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailMenuCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    [cell fillCellWithModel:self.model];
                    cell.priceLab.text = [NSString stringWithFormat:@"共计：¥ %@",[Utilities changeDoubleToString:self.model.userOrderGoodsVoList.firstObject.ogTotal]];
                    cell.countLab.text = [NSString stringWithFormat:@"共%ld桌",(long)self.model.userOrderGoodsVoList.firstObject.ogGoodsNum];
                    
                    return cell;
                } break;
                default:
                    break;
            }

        } break;
        case FieldOrderDetailSectionType_dishes:{

            V2CommonMenuModel *cellModel = self.selDishesArray[indexPath.row];
            V2MenuSingleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"V2MenuSingleCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.bgView.hidden = YES;
            [cell fillWithModel:cellModel status:V2MenuSingleCellStatus_normal];
            return cell;

        } break;
        case FieldOrderDetailSectionType_mark:{

            SerOrderDetailMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailMarkCellID"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.itemContentLab.text = self.model.uoRemark;
            return cell;

        } break;
        case FieldOrderDetailSectionType_payDetails:{

            switch (indexPath.row) {
                case FieldOrderPayDetailCellType_payType:{
                    
                    SerOrderDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.mainTitle.text = @"支付方式";
                    cell.subTitle.hidden = YES;
                    cell.contentLab.textColor = UIColorHex(0x333333);
                    cell.contentLab.text = @"全款支付";


                    return cell;
                    
                } break;
                case FieldOrderPayDetailCellType_price:{
                    
                    SerOrderDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.contentLab.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:(self.model.uodTotal - self.model.uodDeposit - self.model.uodDiscountMcAmount - self.model.uodDiscountPcAmount)]];
                    cell.subTitle.hidden = NO;
                    cell.mainTitle.text = @"套餐价格";
                    cell.subTitle.text = @"";
                    cell.contentLab.textColor = UIColorHex(0x333333);
                    
                    return cell;
                    
                } break;
                case FieldOrderPayDetailCellType_coupon:{
                    
                    SerOrderDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.mainTitle.text = @"优惠券";
                    cell.contentLab.text = [NSString stringWithFormat:@"-¥%@",[Utilities changeDoubleToString:self.model.uodDiscountMcAmount]];
                    cell.contentLab.textColor = UIColorHex(0x333333);
                    cell.subTitle.hidden = YES;

                    return cell;
                    
                } break;
                case FieldOrderPayDetailCellType_pingtaijintie:{
                    
                    SerOrderDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.mainTitle.text = @"平台津贴";
                    cell.contentLab.text = [NSString stringWithFormat:@"-¥%@",[Utilities changeDoubleToString:self.model.uodDiscountPcAmount]];
                    cell.contentLab.textColor = UIColorHex(0x333333);
                    cell.subTitle.hidden = YES;

                    return cell;
                    
                } break;
                case FieldOrderPayDetailCellType_totalPay:{
                    
                    SerOrderDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.mainTitle.text = @"实付金额";
                    cell.contentLab.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:self.model.uodPayAmount]];
                    cell.subTitle.hidden = YES;

                    return cell;
                    
                } break;
                default:
                    break;
            }


        } break;
        case FieldOrderDetailSectionType_orderInfo:{

            switch (indexPath.row) {
                case FieldOrderInfoCellType_orderNo:{
                    
                    OrderDetailNoCopyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderDetailNoCopyCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    cell.itemContentLab.text = self.model.uoOrderNo;
                    
                    cell.OrderDetailNoCopyCellBlock = ^{
                      
                        UIPasteboard *pastboard = [UIPasteboard generalPasteboard];
                        [pastboard setString:self.model.uoOrderNo];
                        
                        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"已复制%@",self.model.uoOrderNo]];
                        [SVProgressHUD dismissWithDelay:1];
                    };
                    
                    return cell;
                    
                } break;
                case FieldOrderInfoCellType_orderCTime:{
                    
                    SerOrderDetailInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailInfoCellID"];
                    cell.selectionStyle =UITableViewCellSelectionStyleNone;
                    
                    cell.mainTitle.text = @"创建时间";
                    cell.contentLab.textColor = UIColorHex(0x333333);
                    cell.contentLab.text = self.model.uoCreateTime;
                    cell.subTitle.hidden = YES;
                    return cell;
                } break;
                default:
                    break;
            }

        } break;
        default:
            break;
    }
    
    SerOrderDetailStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SerOrderDetailStatusCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  [UIColor clearColor];
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        

        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailPackageCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailPackageCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailFieldCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailFieldCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailMenuCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailMenuCellID"];
        
        
        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailStatusCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailStatusCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailContactCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailContactCellID"];

        
        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailSerCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailSerCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailMarkCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailMarkCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"SerOrderDetailInfoCell" bundle:nil] forCellReuseIdentifier:@"SerOrderDetailInfoCellID"];
        
        [_tableview registerNib:[UINib nibWithNibName:@"OrderDetailNoCopyCell" bundle:nil] forCellReuseIdentifier:@"OrderDetailNoCopyCellID"];

        [_tableview registerNib:[UINib nibWithNibName:@"V2MenuSingleCell" bundle:nil] forCellReuseIdentifier:@"V2MenuSingleCellID"];

        
        
    }
    return _tableview;
}

- (CommonOrderDetailModel *)model {
    if (!_model) {
        _model = [[CommonOrderDetailModel alloc] init];
    }
    return _model;
}

//
- (void)request_orderDetail {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_OrderDetail];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.orderId forKey:@"uoId"];
    [dic setValue:@"3" forKey:@"uodOrderType"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"uoUserId"];

    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    weakSelf.model = [CommonOrderDetailModel modelWithDictionary:resultDic];
//                    NSArray *refundFlowList = [resultDic valueForKey:@"refundFlowList"];
                    NSArray *userOrderGoodsVoList = [resultDic valueForKey:@"userOrderGoodsVoList"];
                    NSArray *userOrderBanquetMenuList = [resultDic valueForKey:@"userOrderBanquetMenuList"];
                    
                    if ([userOrderGoodsVoList isKindOfClass:[NSArray class]]) {
                        weakSelf.model.userOrderGoodsVoList = @[].mutableCopy;
                        
                        for (int i = 0; i < userOrderGoodsVoList.count; i++) {
                            
                            NSDictionary *goodsDic = userOrderGoodsVoList[i];
                            CommonOrderGoodsModel *goodsModel = [CommonOrderGoodsModel modelWithDictionary:goodsDic];
                            [weakSelf.model.userOrderGoodsVoList addObject:goodsModel];
                        }
                    }
                    
                    if ([userOrderBanquetMenuList isKindOfClass:[NSArray class]]) {
                        weakSelf.selDishesArray = @[].mutableCopy;
                        
                        for (NSDictionary * userOrderBanquetMenuDic in userOrderBanquetMenuList) {
                            V2CommonMenuModel *model = [V2CommonMenuModel modelWithDictionary:userOrderBanquetMenuDic];
                            [weakSelf.selDishesArray addObject:model];
                        }
                        
                    }
                    
                    [weakSelf.tableview reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}


@end
